import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
// 通过mode加载.env文件

export default defineConfig(({ command, mode }) => {

  // 设置环境变量包地址
  const envDir = '/env'
  // 获取环境变量
  const env = loadEnv(mode, process.cwd()+envDir)

  return {
    envDir: '.'+envDir,
    plugins: [vue(),
    // 自动导入element plus
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    ],
    server: {
      host: '0.0.0.0', // 指定服务器监听的地址，解决“vite use `--host` to expose”问题
      port: 5173, // 默认监听端口：5173
      hmr: true, // 是否进行热更新
      open: true, // 启动后自动打开浏览器
      proxy: {
        // 开发环境跨域代理设置
        [env.VITE_SERVER_API]: {
          target: env.VITE_SERVER,  // 获取数据的服务器地址设置
          changeOrigin: true, // 运行跨域
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
        // ['/api']: {
        //   target: 'http://10.0.62.23:9080/matchCode',  // 获取数据的服务器地址设置
        //   changeOrigin: true, // 运行跨域
        //   rewrite: (path) => path.replace(/^\/api/, ''),
        // },
      },
    },
    resolve: {
      alias: [
        { find: '@', replacement: path.resolve(__dirname, 'src') }
      ],
    },
    //scss全局变量一个配置
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/variable.scss" as * ;`,
        },
      }
    }
  }
});
